﻿using System;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.Framework.Commons;
using IGM.Framework.Constants;

namespace IGM.Presentation.Assignment
{
    public partial class StudentLecturersControl : UserControl
    {
        private readonly Business.Business _business;

        public StudentLecturersControl()
        {
            InitializeComponent();

            _business = Main.Main.Instance.IgmBusiness;
        }

        private void StudentLecturersControlLoad(object sender, EventArgs e)
        {
            LoadMajor();
            LoadLecturer();
            LoadStudentLecturer();
            Dock = DockStyle.Top;
        }

        private void LoadMajor()
        {
            cboMajors.DataSource = _business.GetListMajor();
            cboMajors.DisplayMember = PropertiesConstants.Name;
            cboMajors.ValueMember = PropertiesConstants.Id;
        }

        private void LoadLecturer()
        {
            cboLecturers.DataSource = _business.GetListLecturerByMajorId(((Major)cboMajors.SelectedItem).Id);
            cboLecturers.DisplayMember = PropertiesConstants.Name;
            cboLecturers.ValueMember = PropertiesConstants.Id;
        }

        private void LoadStudent()
        {
            var students = _business.GetStudentNeedLecturer(((Major)cboMajors.SelectedItem).Id);
            txtCountStudents.Text = students.Count.ToString();
            txtCountLecturers.Text = _business.GetListLecturerByMajorId(((Major)cboMajors.SelectedItem).Id).Count.ToString();
            LoadStudentLecturer();
        }


        private void LoadStudentLecturer()
        {
            if (cboMajors.SelectedItem == null) return;
            dgvStudentLecturer.DataSource = _business.GetStudentLecturerByMajorId(((Major)cboMajors.SelectedItem).Id);
            DataGridViewHelper.ShowHideColumns(dgvStudentLecturer,
                                               new[] { PropertiesConstants.IsChecked, "StudentCode", "StudentName", "LecturerName", "Year" });
            DataGridViewHelper.ShowIsCheck(dgvStudentLecturer);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvStudentLecturer, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvStudentLecturer, new[] { "IsChecked", "StudentCode", "StudentName", "LecturerName", "Year" },
                                                   new[] { "Chọn", "Mã sinh viên", "Họ tên", "Giảng viên hướng dẫn", "Năm" });            



            StudentLecturerCurrentRowChanged();
        }

        private void StudentLecturerCurrentRowChanged()
        {
            if (dgvStudentLecturer.CurrentRow != null)
            {
                txtStudentName.Text = dgvStudentLecturer.CurrentRow.Cells["StudentName"].Value.ToString();
                txtStudentCode.Text = dgvStudentLecturer.CurrentRow.Cells["StudentCode"].Value.ToString();
                cboLecturers.Text = dgvStudentLecturer.CurrentRow.Cells["LecturerName"].Value.ToString();
                txtYear.Text = dgvStudentLecturer.CurrentRow.Cells["Year"].Value.ToString();
            }

            else
            {
                txtStudentName.Text = txtStudentCode.Text = txtYear.Text = string.Empty;
                cboLecturers.SelectedIndex = -1;
            }
        }

        private void DgvStudentLecturerCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvStudentLecturer == null ||
               dgvStudentLecturer.Columns[(string)typeof(StudentLecturer).Properties().IsChecked] == null ||
               dgvStudentLecturer.Columns[(string)typeof(StudentLecturer).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvStudentLecturer[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvStudentLecturer[e.ColumnIndex, e.RowIndex].Value;
        }

        private void DgvStudentLecturerCurrentCellChanged(object sender, EventArgs e)
        {
            StudentLecturerCurrentRowChanged();
        }

        private void ReloadStudentLecturer(StudentLecturer studentlecturer)
        {
            LoadStudent();
            DataGridViewHelper.SetDgvCurrentCell(dgvStudentLecturer, studentlecturer.StudentCode);
            StudentLecturerCurrentRowChanged();
        }

        private void BtnEditStudentLecturerClick(object sender, EventArgs e)
        {
            try
            {
                if (dgvStudentLecturer.CurrentRow != null)
                {
                    var studentlecturer = new StudentLecturer
                    {
                        Id = Convert.ToInt32(dgvStudentLecturer.CurrentRow.Cells[PropertiesConstants.Id].Value),
                        IdLecturer = Convert.ToInt32(((Lecturer)cboLecturers.SelectedItem).Id),
                        Year = Convert.ToInt32(dgvStudentLecturer.CurrentRow.Cells["Year"].Value)
                    };
                    string message;
                    if (_business.EditStudentLecturerValidator(studentlecturer, out message))
                    {
                        if (!_business.EditStudentLecturer(studentlecturer))
                        {
                            MessageBox.Show(ConstantMessages.SaveItemNotSuccess, FileConstants.ProgramName);
                            return;
                        }
                        ReloadStudentLecturer(studentlecturer);
                    }
                    else
                        MessageBox.Show(message);
                }
                else
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.ProgramName);
            }

            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        private void ComboBoxSelectionChangeCommitted(object sender, EventArgs e)
        {
            LoadStudent();
        }  
   
    }
}
